package com.kzd.sdk.ui.base;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kzd.sdk.base.OwnerActivity;
import com.kzd.sdk.callback.KZD_WebViewClient;
import com.kzd.sdk.utils.AppUtils;

import static android.widget.LinearLayout.VERTICAL;

public class WebViewActivity extends OwnerActivity {

    private View mTabLayout;
    private LinearLayout mLinearLayout;
    private TextView mTextViewTitle;
    public WebView mWebView;

    public void setTextViewTitle(String title){
        mTextViewTitle.setText(title);
    }

    public void setTabLayoutGone(){
        mTabLayout.setVisibility(View.GONE);
    }

    @Override
    public String getLayoutIdByName() {
        return null;
    }

    @Override
    public View getLayoutView() {
        return createView();
    }

    @SuppressLint("SetJavaScriptEnabled")
    private View createView() {
        mLinearLayout = new LinearLayout(this);
        LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(-1, -1);
        mLinearLayout.setLayoutParams(linearParams);
        mLinearLayout.setOrientation(VERTICAL);
        mLinearLayout.setBackgroundColor(Color.WHITE);

        mTabLayout = LayoutInflater.from(this)
                .inflate(AppUtils.getLayoutId((this), "kzd_include_app_tab"), (null));
        LinearLayout.LayoutParams tabParams = new LinearLayout.LayoutParams(-1, AppUtils.dp2px(45f));
        mTabLayout.setLayoutParams(tabParams);
        mTabLayout.findViewById(AppUtils.getId((this), "ivBackPressed"))
                .setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onBackPressed();
                    }
                });
        mTextViewTitle = mTabLayout.findViewById(AppUtils.getId((this), "tvTitle"));
        mLinearLayout.addView(mTabLayout);

        mWebView = new WebView(this);
        LinearLayout.LayoutParams webParams = new LinearLayout.LayoutParams(-1, -1);
        mWebView.setLayoutParams(webParams);
        mWebView.setHorizontalScrollBarEnabled(false);
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setWebViewClient(new KZD_WebViewClient(this));
        //支持缩放，默认为true。
        mWebView.getSettings().setSupportZoom(false);
        //调整图片至适合webview的大小
//        settings.useWideViewPort = true;
        // 缩放至屏幕的大小
        mWebView.getSettings().setLoadWithOverviewMode(true);
        //设置默认编码
        mWebView.getSettings().setDefaultTextEncodingName("utf-8");
        //设置自动加载图片
        mWebView.getSettings().setLoadsImagesAutomatically(true);
        //允许访问文件
        mWebView.getSettings().setAllowFileAccess(true);
        //是否节点缓存
        mWebView.getSettings().setDomStorageEnabled(true);
        //开启javascript
        mWebView.getSettings().setJavaScriptEnabled(true);
        //支持通过JS打开新窗口
        mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        //支持内容重新布局
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        //关闭webview中缓存
        mWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        // 设置开启定位功能
        mWebView.getSettings().setGeolocationEnabled(true);

        mLinearLayout.addView(mWebView);

        return mLinearLayout;
    }

    @Override
    protected void onDestroy() {
        if (mWebView.getWindowToken() != null) {
            mLinearLayout.removeView(mWebView);
        }
        mWebView.clearHistory();
        mWebView.destroy();
        super.onDestroy();
    }
}
